package com.xuanxu.blogs.module.user.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuanxu.blogs.module.user.entity.dao.UserDao;
import com.xuanxu.blogs.module.user.entity.vo.admin.SelectUserRequestVo;
import com.xuanxu.blogs.module.user.entity.vo.admin.UpdateUserRequestVo;
import com.xuanxu.blogs.module.user.entity.vo.admin.UserResponseVo;
import com.xuanxu.blogs.module.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XuanXu
 * @version 1.0
 * @date 2024-08-19 14:17
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin-api/user")
@Api(tags = "用户相关接口")
public class AdminUserController {

    private final UserService userService;

    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name="currentPage", value = "页码", required = true),
            @ApiImplicitParam(dataType = "Integer", name="pageSize", value = "页面大小", required = true),
    })
    @GetMapping("/page/{currentPage}/{pageSize}")
    @ApiOperation("分页查询用户")
    public ResponseEntity<IPage<UserResponseVo>> pageSelectUser(@PathVariable("currentPage") Long currentPage,
                                                                @PathVariable("pageSize") Long pageSize,
                                                                SelectUserRequestVo selectUserRequestVo) {
        UserDao userDao = BeanUtil.copyProperties(selectUserRequestVo, UserDao.class);

        IPage<UserDao> userDaoPage = userService.pageSelectUser(currentPage, pageSize, userDao);
        Page<UserResponseVo> userResponsePage = new Page<>();
        BeanUtil.copyProperties(userDaoPage, userResponsePage);

        if (userDaoPage != null) {
            List<UserResponseVo> userResponseVoList = BeanUtil.copyToList(userDaoPage.getRecords(), UserResponseVo.class);
            userResponsePage.setRecords(userResponseVoList);
        }
        return ResponseEntity.ok(userResponsePage);
    }


    @PutMapping
    @ApiOperation("根据用户ID修改用户")
    public ResponseEntity<Boolean> updateUserById(@RequestBody UpdateUserRequestVo updateUserRequestVo) {
        UserDao userDao = BeanUtil.copyProperties(updateUserRequestVo, UserDao.class);
        boolean isSuccess = userService.updateUserById(userDao);
        return ResponseEntity.ok(isSuccess);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "userId", value = "用户ID", required = true)
    })
    @GetMapping("/user/{userId}")
    @ApiOperation("根据用户ID查询用户")
    public ResponseEntity<UserResponseVo> selectUserByUserId(@PathVariable("userId") Long userId) {
        UserDao userDao = userService.selectUserByUserId(userId);
        UserResponseVo userResponseVo = BeanUtil.copyProperties(userDao, UserResponseVo.class);
        return ResponseEntity.ok(userResponseVo);
    }
}
